package cn.jh.clearing.business.impl;

import cn.jh.clearing.business.PaymentOrderBusiness;
import cn.jh.clearing.pojo.BrandRateVO;
import cn.jh.clearing.pojo.PaymentOrder;
import cn.jh.clearing.pojo.PaymentQueryCriteria;
import cn.jh.clearing.repository.PaymentOrderRepository;
import cn.jh.clearing.service.ProfitService;
import cn.jh.clearing.util.Util;
import cn.jh.common.exception.ServiceException;
import cn.jh.common.utils.CommonConstants;
import cn.jh.common.utils.DateUtil;
import com.google.common.base.Joiner;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.net.URI;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.apache.commons.lang3.StringUtils.isEmpty;

@Service
public  class PaymentOrderBusinessImpl implements PaymentOrderBusiness{

	private static final Logger LOG = LoggerFactory.getLogger(ProfitService.class);

    private final PaymentOrderRepository paymentOrderRepository;
    private final Util util;
	private final JdbcTemplate jdbcTemplate;

	@Autowired
    public PaymentOrderBusinessImpl(PaymentOrderRepository paymentOrderRepository, Util util, JdbcTemplate jdbcTemplate) {
        this.paymentOrderRepository = paymentOrderRepository;
        this.util = util;
        this.jdbcTemplate = jdbcTemplate;
    }

	@Transactional
	@Override
	public PaymentOrder mergePaymentOrder(PaymentOrder order) {
	    if (order.getId() == null) {
	        paymentOrderRepository.save(order);
        }
		return paymentOrderRepository.saveAndFlush(order);
	}

	@Override
	public PaymentOrder queryPaymentOrderBycode(String ordercode) {
		return paymentOrderRepository.findPaymentOrderByCode(ordercode);
	}

	@Override
	public PaymentOrder queryPaymentOrderByIdAndCode(int orderId,String orderCode) {
		return paymentOrderRepository.findPaymentOrderByIdAndCode(orderId, orderCode);
	}

    @Override
    public Page<PaymentOrder> findPaymentOrder(PaymentQueryCriteria queryCriteria, Pageable pageable) {
        Page<PaymentOrder> pageResult = paymentOrderRepository.findAll(queryCriteria.getSpecification(), pageable);
        if(queryCriteria.getType().equalsIgnoreCase(CommonConstants.ORDER_TYPE_WITHDRAW) && pageResult != null){
            URI uri = util.getServiceUrl("user", "error url request!");
            String url = uri.toString() + "/v1.0/user/brandrate/list";
            MultiValueMap<String, String> requestEntity  = new LinkedMultiValueMap<>();
            List<PaymentOrder> orderList = pageResult.getContent();
            for(PaymentOrder order: orderList){
                requestEntity.add("brand_channel", Joiner.on("-").join(order.getBrandid(), order.getChannelid()));
            }
            RestTemplate restTemplate = new RestTemplate();
            String response = restTemplate.postForObject(url, requestEntity, String.class);
            Gson gs = new Gson();
            List<BrandRateVO> brandRateList = gs.fromJson(response, new TypeToken<List<BrandRateVO>>(){}.getType());
            Map<String, BrandRateVO> brandRateMap = brandRateList.stream().collect(Collectors.toMap(BrandRateVO::brandAndChannel, BrandRateVO -> BrandRateVO));
            for (PaymentOrder order: orderList) {
                BrandRateVO brandRate = brandRateMap.get(Joiner.on("-").join(order.getBrandid(), order.getChannelid()));
                if (brandRate == null) continue;
                order.setRealAmount(order.getRealAmount().subtract(brandRate.getWithdrawFee()));
            }
        }
        return pageResult;
    }

	@Override
	public List<PaymentOrder> findPaymentOrder(PaymentQueryCriteria queryCriteria) {
		return paymentOrderRepository.findAll(queryCriteria.getSpecification());
	}

	@Override
	public PaymentOrder queryPaymentOrderBycodeAndStatus(String ordercode, String status) {
		return paymentOrderRepository.findPaymentOrderByCodeAndStatus(ordercode, status);
	}

	@Override
	public PaymentOrder queryPaymentOderByOutOrdercode(String outordercode) {
		return paymentOrderRepository.findPaymentOrderByOutCode(outordercode);
	}

	@Transactional
	@Override
	public void updateThirdcodeByOrdercode(String thirdcode, String ordercode) {
		paymentOrderRepository.updatePaymentThirdcodeByOrdercode(thirdcode, ordercode);
	}

	@Override
	public PaymentOrder queryPaymentOrderByThirdcode(String thirdcode) {
		return paymentOrderRepository.findPaymentOrderByThirdCode(thirdcode);
	}

	@Override
	public List<PaymentOrder> queryWeekBrandWithdrawRebate(
			Date startDate, Date endDate) {
		return paymentOrderRepository.findBrandWithdrawRebate(startDate, endDate);
	}

	@Override
	public List<PaymentOrder> queryWeekBrandWithdrawClearRebate(
			Date startDate, Date endDate) {
		return paymentOrderRepository.findBrandWithdrawclearRebate(startDate, endDate);
	}

	@Override
	public List<PaymentOrder> queryWaitClearingOrders() {
		//@Query("select paymentOrder from  PaymentOrder paymentOrder where paymentOrder.status = '4'")
		PaymentQueryCriteria queryCriteria = new PaymentQueryCriteria();
		queryCriteria.setStatus("4");
		return paymentOrderRepository.findAll(queryCriteria.getSpecification());
	}

	@Override
	public BigDecimal findsumPaymentOrder(long userid,String[] type  ,String[] status ,String autoClearing){
		
		return paymentOrderRepository.findsumPaymentOrder(userid, type, status,autoClearing);
	}

	@Override
	public List<PaymentOrder> findsumPaymentOrderByDescCode(long userid, String[] type  ,String[] status ,String desccode, Date startTimeDate,Date endTimeDate){
		
		return paymentOrderRepository.findsumPaymentOrderByDescCode(userid, type, status, desccode, startTimeDate, endTimeDate);
	}
	
	
	@Override
	public BigDecimal findsumPaymentOrder(long userid,String[] type  ,String[] status ,String autoClearing ,Date startTimeDate,Date endTimeDate){
		return paymentOrderRepository.findsumPaymentOrder(userid, type, status,autoClearing,startTimeDate,endTimeDate);
	}

	@Override
	public int findsumPaymentOrderCount(long userid,String[] type  ,String[] status ,String autoClearing ,Date startTimeDate,Date endTimeDate){


		return paymentOrderRepository.findsumPaymentOrdercount(userid, type, status,autoClearing,startTimeDate,endTimeDate);
	}
	@Override
	public BigDecimal findsumPaymentOrderBrand(long brand,String[] type  ,String[] status ,String[] autoClearing ,Date startTimeDate,Date endTimeDate){
		return paymentOrderRepository.findsumPaymentOrderBrand(brand, type, status,autoClearing,startTimeDate,endTimeDate);
		
	}
	@Override
	public int findsumPaymentOrderBrandCount(long brand,String[] type  ,String[] status ,String[] autoClearing ,Date startTimeDate,Date endTimeDate){
		
		return paymentOrderRepository.findsumPaymentOrderBrandcount(brand, type, status,autoClearing,startTimeDate,endTimeDate);
	}
	
	@Override
	public BigDecimal findsumPaymentOrderPlatform(String[] type  ,String[] status ,String[] autoClearing ,Date startTimeDate,Date endTimeDate){
		
		return paymentOrderRepository.findsumPaymentOrderPlatform(type, status, autoClearing, startTimeDate, endTimeDate);
	}
	
	@Override
	public int findsumPaymentOrderPlatformCount(String[] type  ,String[] status ,String autoClearing[] ,Date startTimeDate,Date endTimeDate){

		return paymentOrderRepository.findsumPaymentOrderPlatformcount(type, status, autoClearing, startTimeDate, endTimeDate);
	}

	@Transactional
	@Override
	public void updateAutoClearingByOrdercode(String ordercode,
			String autoclearing) {
		paymentOrderRepository.updateAutoClearingByOrdercode(autoclearing, ordercode);
	}

	//根据多个条件查询订单信息
	@Override
	public Map queryPaymentOrderAll(String startTime, String endTime, String phone, String ordercode,
			String ordertype, String orderstatus, long brandid, Pageable pageAble) {
		Map object = new HashMap();
		//将sql语句变成字符串进行拼接
		StringBuffer sql = new StringBuffer("from t_payment_order where 1=1");
		
		StringBuffer sql1 = new StringBuffer(" and order_type=2");
		StringBuffer sql2 = new StringBuffer(" and order_type=1");
		StringBuffer sql3 = new StringBuffer(" and order_type=0");
		StringBuffer sql4 = new StringBuffer(" and order_status=0");
		StringBuffer sql5 = new StringBuffer(" and order_status=1");

		
		
		//如果条件不为空往后面拼接
		if(startTime!=null&&!startTime.equals("")){
			sql.append(" and create_time>='"+startTime+"'");
		}
		
		if(endTime!=null&&!endTime.equals("")){	
			sql.append(" and create_time<='"+endTime+"'");
		}
		
		if(phone!=null&&!phone.equals("")){
			sql.append(" and phone='"+phone+"'");
		}
		
		if(ordercode!=null&&!ordercode.equals("")){
			sql.append(" and order_code='"+ordercode+"'");
		}
		
		
		if(ordertype!=null&&!ordertype.equals("")){
			sql.append(" and order_type='"+ordertype+"'");
		}
		

		if(orderstatus!=null&&!orderstatus.equals("")){
			sql.append(" and order_status='"+orderstatus+"'");
		}
	
		
		if(brandid!=-1){ 
			sql.append(" and brand_id='"+brandid+"'");
		}
		
		//定义一个新的字符串用来拼接之前的字符串
		StringBuffer sqlCount=new StringBuffer("select count(*) as count ").append(sql);
		
		StringBuffer withdrawingamount = null;  //待提现
		StringBuffer withdrawedamount = null;   //已提现
		StringBuffer rechargingamount = null;   //待充值
		StringBuffer rechargedamount = null;    //已充值
		StringBuffer buyamount = null;    //成功的总购买
		StringBuffer withdrawamount = null;    //总提现
		StringBuffer rechargamount = null;    //总充值
		StringBuffer buys = null;    //总购买
		StringBuffer ingamount = null;    //待完成
		StringBuffer edamount = null;  //已完成
		
		
		if((ordertype==null&&orderstatus.equals(""))){
			withdrawingamount = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql1).append(sql4);
			rechargingamount = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql3).append(sql4);
			rechargedamount = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql3).append(sql5);
			withdrawedamount = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql1).append(sql5);
			buyamount = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql2).append(sql5);
			withdrawamount = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql1);
			rechargamount = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql3);
			buys = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql2);
		}
		
		
		if(ordertype!=null||(ordertype!=null&&!orderstatus.equals(""))){
			if(ordertype.equals("2")&&orderstatus.equals("0")){
				//待提现金额
				withdrawingamount = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql1).append(sql4);
			}
			
			if(ordertype.equals("2")&&orderstatus.equals("1")){
				//已提现金额
				withdrawedamount = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql1).append(sql5);
			}
			
			if(ordertype.equals("2")){
				//提现金额
				withdrawamount = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql1);
			}
			if(orderstatus.equals("0")&&ordertype==null){
				//待完成金额
				ingamount = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql4);
			}
			
			if(orderstatus.equals("1")&&ordertype==null){
				//完成金额
				edamount = new StringBuffer("select sum(real_amount) as sum ").append(sql5);
			}
			
			if(ordertype.equals("1")&&orderstatus.equals("1")){
				//总购买成功的金额
				buyamount = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql2).append(sql5);
			}
			
			if(ordertype.equals("0")&&orderstatus.equals("0")){
				//待充值金额
				rechargingamount = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql3).append(sql4);
			}
			
			if(ordertype.equals("0")){
				//充值总金额
				rechargamount = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql3);
			}
			
			if(ordertype.equals("0")&&orderstatus.equals("1")){
				//已充值总金额
				rechargedamount = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql3).append(sql5);
			}
			
			if(ordertype.equals("1")){
				//总购买金额
				buys = new StringBuffer("select sum(real_amount) as sum ").append(sql).append(sql2);
			}
		}
		
		//将字符串变成sql语句查询到的结果转成int类型
		int count=Integer.parseInt(jdbcTemplate.queryForMap(sqlCount.toString()).get("count").toString()) ;
		
		Double withdrawing = 0.00;   //待提现
		Double recharging = 0.00;    //待充值
		Double recharged = 0.00;     //已充值
		Double withdrawed = 0.00;    //已提现
		Double buy = 0.00;    //成功总购买
		Double withdraw = 0.00;    //总提现
		Double recharge = 0.00;    //总充值
		Double allbuy = 0.00;   //总购买
		
		String str = new String();
		String str1 = new String();
		String str2 = new String();
		String str3= new String();
		String str4 = new String();
		String str5 = new String();
		String str6 = new String();
		String str7 = new String();
		
		
		try {
			if((ordertype==null&&orderstatus.equals(""))){
				
				//定义一个map集合存放查询的数据
				
				Map<String, Object> queryForwithdrawing = jdbcTemplate.queryForMap(withdrawingamount.toString());
				Map<String, Object> queryForrecharging = jdbcTemplate.queryForMap(rechargingamount.toString());
				Map<String, Object> queryForrecharged = jdbcTemplate.queryForMap(rechargedamount.toString());
				Map<String, Object> queryForwithdrawed = jdbcTemplate.queryForMap(withdrawedamount.toString());
				Map<String, Object> queryForbuy = jdbcTemplate.queryForMap(buyamount.toString());
				Map<String, Object> queryForwithdraw = jdbcTemplate.queryForMap(withdrawamount.toString());
				Map<String, Object> queryForrecharge = jdbcTemplate.queryForMap(rechargamount.toString());
				Map<String, Object> queryForbuys = jdbcTemplate.queryForMap(buys.toString());
				
				
				if(queryForwithdrawing.get("sum")!=null){
					str = queryForwithdrawing.get("sum").toString();
				}else{
					str = "0.00";
				}
				
				if(queryForrecharging.get("sum")!=null){
					str1 = queryForrecharging.get("sum").toString();
				}else{
					str1= "0.00";
				}
				
				if(queryForrecharged.get("sum")!=null){
					str2 = queryForrecharged.get("sum").toString();
				}else{
					str2 = "0.00";
				}
				
				if(queryForwithdrawed.get("sum")!=null){
					str3 = queryForwithdrawed.get("sum").toString();
				}else{
					str3 = "0.00";
				}
				
				if(queryForbuy.get("sum")!=null){
					str4 = queryForbuy.get("sum").toString();
				}else{
					str4 = "0.00";
				}
				
				if(queryForwithdraw.get("sum")!=null){
					str5 = queryForwithdraw.get("sum").toString();
				}else{
					str5 = "0.00";
				}
				
				if(queryForrecharge.get("sum")!=null){
					str6 = queryForrecharge.get("sum").toString();
				}else{
					str6 = "0.00";
				}
				
				if(queryForbuys.get("sum")!=null){
					str7 = queryForbuys.get("sum").toString();
				}else{
					str7 = "0.00";
				}
				
				
				withdrawing = Double.valueOf(str);
				recharging = Double.valueOf(str1);
				recharged = Double.valueOf(str2);
				withdrawed = Double.valueOf(str3);
				buy = Double.valueOf(str4);
				withdraw = Double.valueOf(str5);
				recharge = Double.valueOf(str6);
				allbuy = Double.valueOf(str7);
			}else{
				if(ordertype.equals("2")){
					withdraw = Double.valueOf(jdbcTemplate.queryForMap(withdrawamount.toString()).get("sum").toString());
				}
				if(ordertype.equals("1")){
					allbuy = Double.valueOf(jdbcTemplate.queryForMap(buys.toString()).get("sum").toString());
				}
				if(ordertype.equals("0")){
					recharge = Double.valueOf(jdbcTemplate.queryForMap(rechargamount.toString()).get("sum").toString());
				}
				
				if(ordertype.equals("2")&&orderstatus.equals("0")){
					withdrawing = Double.valueOf(jdbcTemplate.queryForMap(withdrawingamount.toString()).get("sum").toString());
					recharging = 0.00;
					recharged = 0.00;
					withdrawed = 0.00;
					buy = 0.00;
				}
				if(ordertype.equals("0")&&orderstatus.equals("0")){
					withdrawing = 0.00;
					recharging = Double.valueOf(jdbcTemplate.queryForMap(rechargingamount.toString()).get("sum").toString());
					recharged = 0.00;
					withdrawed = 0.00;
					buy = 0.00;

				}
				if(ordertype.equals("0")&&orderstatus.equals("1")){
					withdrawing = 0.00;
					recharging = 0.00;
					recharged = Double.valueOf(jdbcTemplate.queryForMap(rechargedamount.toString()).get("sum").toString());
					withdrawed = 0.00;
					buy = 0.00;

				}
				if(ordertype.equals("2")&&orderstatus.equals("1")){
					withdrawing = 0.00;
					recharging = 0.00;
					recharged = 0.00;
					withdrawed = Double.valueOf(jdbcTemplate.queryForMap(withdrawedamount.toString()).get("sum").toString());
					buy = 0.00;
				}
				if(ordertype.equals("1")&&orderstatus.equals("1")){
					withdrawing = 0.00;
					recharging = 0.00;
					recharged = 0.00;
					withdrawed = 0.00;
					buy = Double.valueOf(jdbcTemplate.queryForMap(buyamount.toString()).get("sum").toString());
					
				}
				
			}
		} catch (Exception e) {
			LOG.error(e.getMessage());
			object.put("message", "该用户没有订单信息");
		}
		
		
		
		int pageNum = pageAble.getPageSize();
		int currentPage = pageAble.getPageNumber();
		
		//定义一个新的字符串用来表示分页的sql语句
		StringBuffer sqlList=new StringBuffer("select * ").append(sql).append(" order by create_time desc limit "+(currentPage)*pageNum+","+pageNum);
		
		
		
		List<PaymentOrder> list = jdbcTemplate.query(sqlList.toString(), new RowMapper<PaymentOrder>(){
		
			
			@Override
			public PaymentOrder mapRow(ResultSet rs, int rowNum) throws SQLException {
				
				PaymentOrder po = new PaymentOrder();
				po.setCreateTime(DateUtil.getYYMMHHMMSSDateFromStr(rs.getString("create_time")));
				po.setPhone(rs.getString("phone"));
				po.setOrdercode(rs.getString("order_code"));
				po.setType(rs.getString("order_type"));
				po.setStatus(rs.getString("order_status"));
				po.setBrandid(rs.getLong("brand_id"));
				po.setAmount(rs.getBigDecimal("amount"));
				po.setAutoClearing(rs.getString("auto_clearing"));
				po.setBankcard(rs.getString("bank_card"));
				po.setBrandname(rs.getString("brand_name"));
				po.setCarNo(rs.getString("car_no"));
				po.setChannelid(rs.getLong("channel_id"));
				po.setChannelname(rs.getString("channel_name"));
				po.setChannelTag(rs.getString("channel_tag"));
				po.setChannelType(rs.getString("channel_type"));
				po.setDesc(rs.getString("order_desc"));
				po.setDescCode(rs.getString("order_desc_code"));
				po.setExtraFee(rs.getBigDecimal("extra_fee"));
				po.setOpenid(rs.getString("openid"));
				po.setOutMerOrdercode(rs.getString("out_mer_order_code"));
				po.setOutNotifyUrl(rs.getString("out_notify_url"));
				po.setOutReturnUrl(rs.getString("out_return_url"));
				po.setPhoneBill(rs.getString("phone_bill"));
				po.setRate(rs.getBigDecimal("rate"));
				po.setRealAmount(rs.getBigDecimal("real_amount"));
				po.setRemark(rs.getString("remark"));
				po.setThirdlevelid(rs.getString("third_level_id"));
				po.setThirdOrdercode(rs.getString("third_order_code"));
				po.setUpdateTime(DateUtil.getYYMMHHMMSSDateFromStr(rs.getString("update_time")));
				po.setUserid(rs.getLong("user_id"));
				
				
				return po;
			}
			
			
						
			
		});
		
		
		object.put("pageNum", pageNum);  //每页显示条数
		object.put("currentPage", currentPage);   //当前页
		object.put("totalElements", count); //总条数
		if(pageNum!=0){
			object.put("totalPages", count/pageAble.getPageSize());  //总页数
		}
		object.put("content", list);
		object.put("withdrawing", withdrawing);  //待提现
		object.put("recharging", recharging);   //待充值
		object.put("recharged", recharged);     //已充值
		object.put("withdrawed", withdrawed);    //已提现
		object.put("buy", buy);   //成功的总购买
		object.put("withdraw", withdraw);     //总提现
		object.put("recharge", recharge);     //总充值
		object.put("allbuy", allbuy);    //总购买
		return object;
		
	}

	@Override
	public PaymentOrder updatePaymentOrder(String orderCode, String orderNo) {
		
		PaymentOrder result = null;
		
		PaymentOrder oldCode = paymentOrderRepository.findByOrdercode(orderCode);
		if(oldCode!=null){
			oldCode.setOrdercode(orderNo);
			result = paymentOrderRepository.save(oldCode);
		}else {
			return null;
		}
		return result;
	}



	//依据用户phone获取用户该时间段内所有order
	@Override
	public List<PaymentOrder> findOrderByphoneAndbrandid(String phone, long brandid, Date startTimeDate, Date endTimeDate,Pageable pageable) {
		return paymentOrderRepository.findOrderByphoneAndbrandid(phone, brandid, startTimeDate, endTimeDate, pageable);
	}




//	根据设置金额,筛选出符合条件的userId
	@Override
	public List<Long> queryUserIdsByAmount(Long brandId, BigDecimal limitAmount, Long autoRebateConfigId) {
		return paymentOrderRepository.queryUserIdsByAmount(brandId, limitAmount, autoRebateConfigId);
	}




	//调用此方法获取待结算订单
	@Override
	public List<PaymentOrder> findOrderByUseridAndStatus(long userid, String[] status) {
		return paymentOrderRepository.findOrderByUseridAndStatus(userid, status);
	}





	@Override
	public List<Object[]> findSumByUserIds(long[] userIds, String type, String status, String autoClearing) {
		return paymentOrderRepository.findSumByUserIds(userIds,type,status,autoClearing);
	}




	@Transactional
	@Override
	public void addErrorByOrderCode(String ordercode, String remark) {
		paymentOrderRepository.updateOrderCodeMsg(ordercode,remark);
	}

	/**
	 * 分页查询提现记录
	 * @param userId
	 * @param page
	 * @param size
	 * @return
	 * @throws ServiceException
	 */
	@Override
	public Object getWithDrawPage(String status, long userId, int page, int size) throws ServiceException {
		/* 1、获取请求模块地址 */
		URI uri = util.getServiceUrl("paymentchannel", "error url request!");
		String url = uri + "/v1.0/paymentchannel/freeQuick/findFreeQuickWithdrawPage";

		/* 2、封装请求参数 */
		MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
		requestEntity.add("userId", userId);
		requestEntity.add("payStatus", status);
		requestEntity.add("payType", "3");//提现
		requestEntity.add("page", page);
		requestEntity.add("size", size);

		/* 3、创建连接 */
		RestTemplate restTemplate = new RestTemplate();
		String result = restTemplate.postForObject(url, requestEntity, String.class);

		/* 4、处理返回结果 */
		if (isEmpty(result)) {
			throw new ServiceException("提现记录查询失败，请稍后再试！");
		}
		JSONObject jsonObject = JSONObject.fromObject(result);
		String resultStr = jsonObject.get("result").toString();
		jsonObject = JSONObject.fromObject(resultStr);


		return jsonObject;
	}

	@Override
	public Object getRebatePage(String token, int page, int size) throws ServiceException {
		/* 1、获取请求模块地址 */
		URI uri = util.getServiceUrl("user", "error url request!");
		String url = uri + "/v1.0/user/userAccount/queryRebatePage/" + token;

		/* 2、封装请求参数 */
		MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
		requestEntity.add("page", page);
		requestEntity.add("size", size);

		/* 3、创建连接 */
		RestTemplate restTemplate = new RestTemplate();
		String result = restTemplate.postForObject(url, requestEntity, String.class);

		/* 4、处理返回结果 */
		if (isEmpty(result)) {
			throw new ServiceException("分润记录查询失败，请稍后再试！");
		}
		JSONObject jsonObject = JSONObject.fromObject(result);
		String respCode = jsonObject.get("resp_code").toString();
		if (CommonConstants.FALIED.equals(respCode)) {
			throw new ServiceException("分润记录查询失败，请稍后再试！");
		}
		String resultStr = jsonObject.get("result").toString();
		jsonObject = JSONObject.fromObject(resultStr);

		return jsonObject;
	}

	@Override
	public Object getRechargePage(String status, long userId, int page, int size) throws ServiceException {
		/* 1、获取请求模块地址 */
		URI uri = util.getServiceUrl("paymentchannel", "error url request!");
		String url = uri + "/v1.0/paymentchannel/freeQuickPay/rechargePage";

		/* 2、封装请求参数 */
		MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
		requestEntity.add("userId", userId);
		requestEntity.add("status", status);//提现状态
		requestEntity.add("page", page);
		requestEntity.add("size", size);

		/* 3、创建连接 */
		RestTemplate restTemplate = new RestTemplate();
		String result = restTemplate.postForObject(url, requestEntity, String.class);

		/* 4、处理返回结果 */
		if (isEmpty(result)) {
			throw new ServiceException("充值记录查询失败，请稍后再试！");
		}
		JSONObject jsonObject = JSONObject.fromObject(result);
		String respCode = jsonObject.get("resp_code").toString();
		if (CommonConstants.FALIED.equals(respCode)) {
			throw new ServiceException("充值记录查询失败，请稍后再试！");
		}
		String resultStr = jsonObject.get("result").toString();
		jsonObject = JSONObject.fromObject(resultStr);

		return jsonObject;
	}

	/**
	 * 分页查询支付记录
	 * @param status
	 * @param userId
	 * @param page
	 * @param size
	 * @return
	 * @throws ServiceException
	 */
	@Override
	public Object getPayPage(String status, long userId, int page, int size) throws ServiceException {
		/* 1、获取请求模块地址 */
		URI uri = util.getServiceUrl("paymentchannel", "error url request!");
		String url = uri + "/v1.0/paymentchannel/freeQuick/findPassagewayOrderPage";

		/* 2、封装请求参数 */
		MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
		requestEntity.add("userId", userId);
		requestEntity.add("payStatus", status);//提现状态
		requestEntity.add("payType", "1");//支付类型
		requestEntity.add("page", page);
		requestEntity.add("size", size);

		/* 3、创建连接 */
		RestTemplate restTemplate = new RestTemplate();
		String result = restTemplate.postForObject(url, requestEntity, String.class);

		/* 4、处理返回结果 */
		if (isEmpty(result)) {
			throw new ServiceException("充值记录查询失败，请稍后再试！");
		}
		JSONObject jsonObject = JSONObject.fromObject(result);
		String respCode = jsonObject.get("resp_code").toString();
		if (CommonConstants.FALIED.equals(respCode)) {
			throw new ServiceException("充值记录查询失败，请稍后再试！");
		}
		String resultStr = jsonObject.get("result").toString();
		jsonObject = JSONObject.fromObject(resultStr);

		return jsonObject;
	}

}
